package leetCode.firstWeek;

public class Code_7 {
  public int reverse(int x) {
    String tmp = "" + x;
    String finalRes = "";
    int isMinus = 0, zeroSum = 0;
    int returnNum;
    Character[] resTmpChar = new Character[tmp.length()];
    for (int i = tmp.length() - 1, j = 0; i >= 0; i--, j++) {
      resTmpChar[j] = tmp.charAt(i);
    }
    if (resTmpChar[tmp.length() - 1].equals('-')) {
      isMinus = 1;
    }
    while (zeroSum < tmp.length() - 1 && resTmpChar[zeroSum].equals('0')) {
      zeroSum++;
    }
    finalRes += (isMinus == 0 ? "" : '-');
    for (int i = zeroSum; i < tmp.length() - isMinus; i++) {
      finalRes += resTmpChar[i];
    }
    try {
      returnNum = Integer.parseInt(finalRes);
    } catch (NumberFormatException e) {
      return 0;
    }
    return returnNum;
  }
}
